package com.bdmc.smokesensor.services;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;

import com.bdmc.smokesensor.Utils.CommonUtil;
import com.bdmc.smokesensor.dao.UserJPA;
import com.bdmc.smokesensor.entity.User;
import com.bdmc.smokesensor.iservices.IUserService;
import com.bdmc.smokesensor.models.TestUser;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;


@Service
public class UserService extends BaseService<User> implements IUserService {

    @Autowired
    private UserJPA userjpa;

    @Override
    public List<User> findAll() {
        String sql="select a.* from t_user a";
        List<User> list = GetListBySql(sql, User.class);
       return list; 
    }
    @Override
    public Map<String,Object> findBySqlPageable(){
        String sql="select a.* from t_user a";
        List<User> p = GetListBySqlPageable(sql, User.class, 2, 3);
        String sql1="select u.id,u.loginName,u.mobile,r.roleName from sys_user u left join rbac_role r on r.id=u.rbac_RoleId";
        //List l = GetListBySql(sql1);
        EntityManager em = emf.createEntityManager();
        //javax.persistence.Query query = em.createNamedQuery("TestUser.roletest");
        javax.persistence.Query query =em.createNativeQuery(sql1);
        List<TestUser> tu = new ArrayList<TestUser>();
        List<Object[]> ro = query.getResultList();
        try {
            
            tu = CommonUtil.castEntity(ro, TestUser.class); 
        } catch (Exception e) {
            System.out.println("error:"+e.toString());
            //TODO: handle exception
        }
        //query.unwrap(NativeQueryImpl.class).addEntity(TestUser.class).setResultTransformer(Transformers.aliasToBean((TestUser.class)));
        //setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        //l.stream().collect(Collectors.toMap(keyMapper, valueMapper))
        //List<TestUser> tu = query.getResultList();
        int total = GetCountBySql(sql);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("total", total);
        map.put("data", tu);
       return map; 
    }

    @Override
    public User findOne(Example<User> example) {
        return userjpa.findOne(example).orElse(null);
	}

	@Override
	public User findOneById(int id) {
		return userjpa.findById(id).orElse(null);
	}

    @Override
    public User save(User user) {
        return userjpa.save(user);
    }

    @Override
    public void deleteById(int id){
        userjpa.deleteById(id);
    }

    @Override
    public List<Map<String, Object>> getMapData() {
        String sql ="select t.t_name as name,t.t_age as age ,1 as ttype from t_user t";
        //return userjpa.getMapDataTest();//@hql 返回map的方式
        
       //List<Map<String,Object>> map = GetListBySql("select t.t_name as name,t.t_age as age from t_user t");//原生sql 返回map 的方式
       return getListMapBySql(sql,2,3);
    }



}